package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.cloudcc.cloudframe.adapter.NewCommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 自定义列表内容显示适配器
 * 
 * @author lhy 2015/11
 */
public class MultipleListAdapter extends NewCommonAdapter<StringBuffer> {
	private boolean canVisbaleArrow;
	private boolean isname;
	private TextView textView;
	private ImageView imageView;
	private  List<StringBuffer> ListisNmae;
	private int id;
	 LinearLayout ll;
	public MultipleListAdapter(Context mContext, boolean canVisbaleArrow) {
		super(mContext);
		this.canVisbaleArrow = canVisbaleArrow;
        
	}
public void setIsname(boolean isname){
	this.isname = isname;

}
public void setname( List<StringBuffer> ListisNmae){
	this.ListisNmae = ListisNmae;
}
	public void setId(int id){
		this.id = id;

	}
int i = 0;
	@Override
	public void convert(ViewHolder holder, StringBuffer t,int position) {
//		holder.setVisibility(R.id.can_imageview, canVisbaleArrow ? View.VISIBLE
//				: View.INVISIBLE);
//		LinearLayout layout = holder.getView(R.id.add_textview_layout);
//		layout.removeAllViews();
//		String[] textarr = t.toString().split(";");
//		// initchance(textarr, layout);
//		initmulti(textarr, layout);
//
//	
	holder.setVisibility(R.id.can_imageview, canVisbaleArrow ? View.VISIBLE
			: View.GONE);
	holder.setVisibility(R.id.isname_ll, isname ? View.VISIBLE
				: View.GONE);
		textView = holder.getView(R.id.isname_name);
//		for (int i = 0; i < ListisNmae.size(); i++) {
//			textView.setText(ListisNmae.get(i));
//		}
		try {
                holder.setText(R.id.isname_name,String.valueOf(ListisNmae.get(position)));
		}catch (Exception ex){

		}

	LinearLayout layout = holder.getView(R.id.add_textview_layout);
	imageView = holder.getView(R.id.isanme_image);
		if(0!=id){
			try {
				Class drawable = R.drawable.class;
				String bgid = "cloudtab" + (id+2) + "_1";
				Field field2 = drawable.getField(bgid);
				int drawableId = field2.getInt(field2.getName());
				imageView.setImageResource(drawableId);
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}

		}

//	textView = (TextView) layout.findViewById(R.id.isname_name);
//	ll =  (LinearLayout) layout.findViewById(R.id.isname_ll);
	layout.removeAllViews();
	String[] textarr = t.toString().split(";");
	// initchance(textarr, layout);
	initmulti(textarr, layout);

}

	@SuppressWarnings("ResourceType")
	public void initmulti(String[] textarr, LinearLayout layout) {
//		if(isname){
//			ll.setVisibility(View.VISIBLE);
//			textView.setText(isName);
//		}else{
//			ll.setVisibility(View.GONE);
//		}

		layout.setOrientation(LinearLayout.HORIZONTAL);
		TextView[] textitem = new TextView[textarr.length];
		TextView[] textitem2 = new TextView[textarr.length];
		TextView[] textitem3 = new TextView[textarr.length];
		LinearLayout linear1 = new LinearLayout(mContext);
		linear1.setId(01);
		linear1.setOrientation(LinearLayout.VERTICAL);
		linear1.setGravity(Gravity.RIGHT);
		LinearLayout linear2 = new LinearLayout(mContext);
		linear2.setId(02);
		linear2.setOrientation(LinearLayout.VERTICAL);
		LayoutParams linparams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		layout.addView(linear1, linparams);
		layout.addView(linear2, linparams);
		for (int i = 0; i < textarr.length - 1; i++) {
			String[] tex;
//            textView.setText(String.valueOf(ListisNmae.get(i)));
			if (textarr[i].contains("/:")) {
				tex = textarr[i].split("/:");
				textitem[i] = new TextView(mContext);
//				textitem[i].setMaxWidth(AppContext.screenW * 1 / 2);
				textitem[i].setWidth(350);
				textitem[i].setId(1000 + i);
				textitem[i].setGravity(Gravity.LEFT);
				textitem[i].setText(tex[0] + " ： ");
				textitem[i].setTextColor(mContext.getResources().getColor(R.color.biaoqian));
				textitem[i].setTextSize(16);
				textitem[i].setSingleLine();
				textitem[i].setEllipsize(TruncateAt.END);
				textitem2[i] = new TextView(mContext);
				textitem2[i].setId(100 + i);
				if (tex.length == 1) {
					textitem2[i].setText("");
				} else {
					textitem2[i].setText(tex[1]);
				}
				textitem2[i].setTextColor(mContext.getResources().getColor(R.color.neirong));
				textitem2[i].setTextSize(16);
				textitem2[i].setSingleLine();
				textitem2[i].setEllipsize(TruncateAt.valueOf("END"));
				LayoutParams params = new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				LayoutParams params2 = new LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				linear1.addView(textitem[i], params);
				linear2.addView(textitem2[i], params2);
			}
			else if(!isname){
				// tex = textarr[i].split(",");
				textitem3[0] = new TextView(mContext);
				textitem3[0].setMaxWidth(AppContext.screenW * 1 / 2);
				textitem3[0].setId(1000 + 1);
				textitem3[0].setText(textarr[0]);
				textitem3[0].setTextColor(Color.GRAY);
				textitem3[0].setTextSize(16);
				textitem3[0].setSingleLine();
				textitem3[0].setEllipsize(TruncateAt.START);
				LayoutParams params = new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				// LinearLayout.LayoutParams params2 = new LayoutParams(
				// LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				linear1.addView(textitem3[0], params);
			}
		}
		// if(textarr.length == 1){
		// for (int i = 0; i < textarr.length; i++) {
		// Log.d("textxxx", "sdc::"+textarr[i]);
		// }
		// textitem3[0] = new TextView(mContext);
		// textitem3[0].setMaxWidth(AppContext.screenW * 1 / 2);
		// textitem3[0].setId(1000 + 1);
		// textitem3[0].setText(textarr[0]);
		// textitem3[0].setTextColor(Color.GRAY);
		// textitem3[0].setTextSize(16);
		// textitem3[0].setSingleLine();
		// textitem3[0].setEllipsize(TruncateAt.START);
		// LinearLayout.LayoutParams params = new LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// // LinearLayout.LayoutParams params2 = new LayoutParams(
		// // LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		// linear1.addView(textitem3[0], params);
		// // linear2.addView(textitem2[0], params2);
		//
		//
		// }

	}

	public void initchance(String[] textarr, LinearLayout layout) {

		TextView[] textitem = new TextView[textarr.length];
		TextView[] textitem2 = new TextView[textarr.length];
		LinearLayout[] linear = new LinearLayout[textarr.length];
		for (int i = 0; i < textarr.length - 1; i++) {
			linear[i] = new LinearLayout(mContext);
			linear[i].setId(10000 + i);
			linear[i].setWeightSum(5);
			linear[i].setOrientation(LinearLayout.HORIZONTAL);
			String[] tex = textarr[i].split("/:");
			textitem[i] = new TextView(mContext);
			textitem[i].setId(1000 + i);
			textitem[i].setText(tex[0] + " ： ");
			textitem[i].setTextColor(Color.GRAY);
			textitem[i].setTextSize(18);
			textitem[i].setGravity(Gravity.RIGHT);
			textitem[i].setSingleLine();
			textitem[i].setEllipsize(TruncateAt.START);
			textitem2[i] = new TextView(mContext);
			textitem2[i].setId(100 + i);
			if (tex.length == 1) {
				textitem2[i].setText("");
			} else {
				textitem2[i].setText(tex[1]);
			}
			textitem2[i].setTextColor(Color.BLACK);
			textitem2[i].setTextSize(16);
			textitem2[i].setSingleLine();
			textitem2[i].setEllipsize(TruncateAt.END);
			LayoutParams linparams = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			LayoutParams params = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 2);
			LayoutParams params2 = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 3);
			linear[i].addView(textitem[i], params2);
			linear[i].addView(textitem2[i], params);
			layout.addView(linear[i], linparams);

		}
	}

	@Override
	public int getLayoutId() {
		return R.layout.multiple_list_layout;
	}

}
